﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kugar.Core.Configuration;
using Kugar.Core.Web;
using Kugar.MVC.DaemonUI.Controllers;
using WS.FlowAuth.Web.Areas.Admin.Filters;
using WS.FlowAuth.Web.BLL;
using WS.FlowAuth.Web.Data.DTO;

namespace WS.FlowAuth.Web.Areas.Admin.Controllers
{
    public class HomeController : AdminBaseController
    {
        public ActionResult UserLogin()
        {
            ViewBag.UserName = "";
            ViewBag.Password = "";

            return View();
        }

        [HttpPost,ActionName("UserLogin")]
        public ActionResult UserLoginPost()
        {
            var userName = Request.GetString("UserName");
            var password = Request.GetString("Password");

            var u1 = WebBLL.User.GetUserByUserNamePassword(userName, password);
            
            //var u = CustomConfigManager.Default.AppSettings.GetValueByName("LoginUserName");
            //var p= CustomConfigManager.Default.AppSettings.GetValueByName("LoginPassword");

            if (u1==null)
            {
                MsgBox("用户名或密码错误,请检查");
                ViewBag.UserName = userName;
                ViewBag.Password = password;

                return View();
            }
            else
            {
                Session["CurrentUser"] = u1;

                var backUrl = Request.GetString("backurl");

                if (string.IsNullOrWhiteSpace(backUrl))
                {
                    backUrl = "/Admin/";
                }

                return Redirect(backUrl);
                //MsgBoxAndGoto("","/Admin/");
            }

            
        }

        public ActionResult UserLogout()
        {
            Session["CurrentUser"] = null;

            return Redirect("/Admin/Home/UserLogin");

            return View();
        }

        [HttpGet]
        [UserAuthority]
        public ActionResult ChangePassword()
        {
            ViewBag.OldPassword = "";
            ViewBag.NewPassword = "";
            ViewBag.ConfimPassword = "";

            return View();
        }

        [UserAuthority]
        [HttpPost,ActionName("ChangePassword")]
        public ActionResult ChangePasswordPost()
        {
            var oldPassword = Request.GetString("OldPassword");
            var newPassword = Request.GetString("NewPassword");
            var confimPassword = Request.GetString("ConfimPassword");

            if (string.IsNullOrWhiteSpace(oldPassword))
            {
                ModelState.AddModelError("OldPassword","旧密码不能为空");
            }

            if (string.IsNullOrWhiteSpace(newPassword))
            {
                ModelState.AddModelError("NewPassword", "新密码不能为空");
            }

            if (newPassword!=confimPassword)
            {
                ModelState.AddModelError("ConfimPassword", "请确保两次输入的密码一致");
            }

            if (ModelState.IsValid)
            {
                var ret = WebBLL.User.ResetUserPassword(WebBLL.User.CurrentUser.UserID, oldPassword, newPassword);

                if (ret.IsSuccess)
                {
                    MsgBoxAndGoto("修改成功", "/Admin/Home/UserLogout");
                }
                else
                {
                    MsgBox("更新失败:" + ret.Message);

                    ViewBag.OldPassword = oldPassword;
                    ViewBag.NewPassword = "";
                    ViewBag.ConfimPassword = "";

                }
            }
            else
            {
                ViewBag.OldPassword = oldPassword;
                ViewBag.NewPassword = "";
                ViewBag.ConfimPassword = "";

                MsgBox("请检查输入数据");
            }
            
            return View();
        }
    }
}